﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using System.Data.SqlClient;
using System.Data.OleDb;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DAL;

namespace KPIS.GERP.GBMS.DAL
{
    public sealed class ProjectRequestOptionDAL : IProjectRequestOptionDAL
    {
       
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public ProjectRequestOptionDAL()
        {

        }

        public void Insert(ProjectExpenseInfo pj)
        {
            //string strSQL;

            //try
            //{
            //    dbManager.Open();
            //    dbManager.CreateParameters(4);

            //    dbManager.AddParameters(0, "@reqSeq", reqSeq);
            //    dbManager.AddParameters(1, "@exp", exp);
            //    dbManager.AddParameters(2, "@status", SystemInfo._WorkRecordStatus);
            //    dbManager.AddParameters(3, "@user", user);


            //    strSQL = "INSERT INTO BGM_REQUEST_EXPENSE(BGM_REQ_SEQ, BGM_EXP_SEQ, BGM_SOURCE_SEQ, RECORD_STATUS, CREATED_BY)"
            //        + " VALUES(@reqSeq, @exp, (select eg.bgm_source_seq from bgm_mas_expense e inner join bgm_mas_expense_group eg on e.bgm_exp_grp_seq = eg.bgm_exp_grp_seq where e.bgm_exp_seq = @exp), @status, @user)";

            //    return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            //}
            //catch
            //{
            //    return 0;
            //}
            //finally
            //{
            //    dbManager.Dispose();
            //}
        }

        public void Update(ProjectExpenseInfo pj)
        {
            //string strSQL;

            //try
            //{
            //    dbManager.Open();
            //    dbManager.CreateParameters(9);

            //    dbManager.AddParameters(0, "@reqExpSeq", reqExpSeq);
            //    dbManager.AddParameters(1, "@reqExpDescr", reqExpDescr);
            //    dbManager.AddParameters(2, "@reqExpPrice", reqExpPrice);
            //    dbManager.AddParameters(3, "@reqExpQty", reqExpQty);
            //    dbManager.AddParameters(4, "@reqExpMtef1", reqExpMtef1);
            //    dbManager.AddParameters(5, "@reqExpMtef2", reqExpMtef2);
            //    dbManager.AddParameters(6, "@reqExpMtef3", reqExpMtef3);
            //    dbManager.AddParameters(7, "@reqExpResource", reqExpResource);
            //    dbManager.AddParameters(8, "@status", SystemInfo._WorkRecordStatus);

            //    strSQL = "UPDATE BGM_REQUEST_EXPENSE"
            //        + " SET BGM_REQ_EXP_DESCR = @reqExpDescr"
            //            + ", BGM_REQ_EXP_PRICE = @reqExpPrice"
            //            + ", BGM_REQ_EXP_QTY = @reqExpQty"
            //            + ", BGM_REQ_EXP_MTEF1 = @reqExpMtef1"
            //            + ", BGM_REQ_EXP_MTEF2 = @reqExpMtef2"
            //            + ", BGM_REQ_EXP_MTEF3 = @reqExpMtef3"
            //            + ", ICM_RESOURCE_LIST_SEQ = @reqExpResource"
            //            + ", RECORD_STATUS = @status"
            //            + ", UPDATED_WHEn = GetDate()"
            //        + " WHERE BGM_REQ_EXP_SEQ = @reqExpSeq";

            //    return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            //}
            //catch
            //{
            //    return 0;
            //}
            //finally
            //{
            //    dbManager.Dispose();
            //}
        }

        public void Delete(ProjectExpenseInfo pj)
        {
            //string strSQL;

            //try
            //{
            //    dbManager.Open();
            //    dbManager.CreateParameters(3);

            //    dbManager.AddParameters(0, "@seq", seq);
            //    dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
            //    dbManager.AddParameters(2, "@updated_by", updated_by);

            //    strSQL = "UPDATE BGM_REQUEST_EXPENSE"
            //        + " SET RECORD_STATUS = @record_status"
            //            + ", UPDATED_BY = @updated_by"
            //            + ", UPDATED_WHEN = GetDate()"
            //        + " WHERE BGM_REQ_EXP_SEQ = @seq";

            //    return dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            //}
            //catch
            //{
            //    return 0;
            //}
            //finally
            //{
            //    dbManager.Dispose();
            //}
        }

        public IList<ProjectOptionInfo> Search(ProjectOptionInfo pj)
        {
            IList<ProjectOptionInfo> pjInfo = new List<ProjectOptionInfo>();

            string strSQL;
            int pjSeq = 0;
            int opType = 0;
            int opSeq = 0;

            if (pj.Project != null)
                pjSeq = pj.Project.Sequence;

            if (pj.Option != null)
            {
                opSeq = pj.Option.Sequence;
                opType = pj.Option.Type;
            }

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@seq", pj.Sequence);
                dbManager.AddParameters(1, "@pjSeq", pjSeq);
                dbManager.AddParameters(2, "@opSeq", opSeq);
                dbManager.AddParameters(3, "@opType", opType);

                strSQL = "SELECT ro.BGM_REQ_OPT_SEQ, ro.DESCR, ro.WEIGHT"
                        + ", ro.BGM_OPTION_SEQ, mo.BGM_OPTION_NAME"
                    + " FROM BGM_REQUEST_OPTION ro"
                        + " INNER JOIN BGM_MAS_OPTION mo on ro.BGM_OPTION_SEQ = mo.BGM_OPTION_SEQ"
                    + " WHERE 0=0"
                        + ((pj.Sequence > 0) ? " AND ro.BGM_REQ_OPT_SEQ = @seq" : "")
                        + ((pjSeq > 0) ? " AND ro.BGM_REQ_SEQ = @pjSeq" : "")
                        + ((opSeq > 0) ? " AND ro.BGM_OPTION_SEQ = @opSeq" : "")
                        + ((opType > 0) ? " AND mo.BGM_OPTION_TYPE_SEQ = @opType" : "");

                DataTable dtb = dbManager.ExecuteDataTable(CommandType.Text, strSQL);

                foreach (DataRow dr in dtb.Rows)
                {
                    //------------------------------ Mas Option ----------------------------------------

                       OptionInfo optInfo = new OptionInfo();

                       optInfo.Sequence = Convert.ToInt32(dr["BGM_OPTION_SEQ"]);
                       optInfo.Name = Convert.ToString(dr["BGM_OPTION_NAME"]);

                       ProjectOptionInfo req = new ProjectOptionInfo();
                       req.Sequence = Convert.ToInt32(dr["BGM_REQ_OPT_SEQ"]);
                       req.Description = Convert.ToString(dr["DESCR"]);
                       req.Weight = Convert.ToInt32(dr["WEIGHT"]);
                       req.Option = optInfo;
                    
                    pjInfo.Add(req);
                }
                //while (rdr.Read())
                //{
                //    // ------------------------------ Mas Option ----------------------------------------

                //    OptionInfo optInfo = new OptionInfo();

                //    optInfo.Sequence = rdr.GetInt32(3);
                //    optInfo.Name = rdr.GetString(4);

                //    ProjectOptionInfo req = new ProjectOptionInfo();

                //    req.Sequence = rdr.GetInt32(0);
                //    req.Description = (rdr.IsDBNull(1)) ? "" : rdr.GetString(1);
                //    req.Weight = rdr.GetDouble(2);
                //    req.Option = optInfo;

                //    pjInfo.Add(req);
                //}

                return pjInfo;
            }
            catch
            {
                return null;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
    }
}
